package com.hwh.traffic.fragment;


import android.os.Build;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.baidu.mapapi.model.LatLng;
import com.hwh.traffic.MapApplication;
import com.hwh.traffic.R;
import com.hwh.traffic.busEntity.Like;
import com.hwh.traffic.db.TrafficLab;
import com.hwh.traffic.utils.BusApiUtil;
import com.hwh.traffic.utils.DensityUtil;

import java.util.List;

public class MylikeFragment extends Fragment {

    private TextView mTitleText;
    private TextView mRouteText;
    private TextView mStopText;
    private LinearLayout mBus_like_list;
    private MapApplication mapApplication;
    private TrafficLab trafficLab;
    private LatLng latLng;

    public MylikeFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mylike, container, false);

    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mapApplication = (MapApplication) getActivity().getApplication();
        trafficLab = mapApplication.getTrafficLab();
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mTitleText = getView().findViewById(R.id.text_title);
        mTitleText.setText("我的收藏");
        mRouteText = getView().findViewById(R.id.like_route);
        mStopText =  getView().findViewById(R.id.like_stop);

        mRouteText.setTextColor(getResources().getColor(R.color.red));
        mRouteText.setBackground(getResources().getDrawable(R.drawable.solid_line_red));

        mRouteText.setOnClickListener(v -> {
            mRouteText.setTextColor(getResources().getColor(R.color.red));
            mRouteText.setBackground(getResources().getDrawable(R.drawable.solid_line_red));

            mStopText.setTextColor(getResources().getColor(R.color.black));
            mStopText.setBackground(getResources().getDrawable(R.drawable.solid_line_bott));
        });

        mStopText.setOnClickListener(v -> {
            mStopText.setTextColor(getResources().getColor(R.color.red));
            mStopText.setBackground(getResources().getDrawable(R.drawable.solid_line_red));

            mRouteText.setTextColor(getResources().getColor(R.color.black));
            mRouteText.setBackground(getResources().getDrawable(R.drawable.solid_line_bott));
        });

        mBus_like_list = getView().findViewById(R.id.bus_like_list);

        new Thread(()->{
            List<Like> allLike = trafficLab.findAllLike();
            getActivity().runOnUiThread(()->{
                for (Like like : allLike) {
                    LinearLayout mLinearLayout = new LinearLayout(getActivity().getApplicationContext());
                    ImageView mImageView = new ImageView(getActivity().getApplicationContext());
                    TextView mTextView = new TextView(getActivity().getApplicationContext());

                    mBus_like_list.addView(mLinearLayout);
                    mLinearLayout.addView(mImageView);
                    mLinearLayout.addView(mTextView);

                    ViewGroup.LayoutParams lp_LinearLayout = mLinearLayout.getLayoutParams();
                    lp_LinearLayout.width = ViewGroup.LayoutParams.MATCH_PARENT;
                    lp_LinearLayout.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
                    mLinearLayout.setGravity(Gravity.CENTER);
                    mLinearLayout.setLayoutParams(lp_LinearLayout);

                    ViewGroup.LayoutParams lp_ImageView = mImageView.getLayoutParams();
                    lp_ImageView.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                    lp_ImageView.height = ViewGroup.LayoutParams.MATCH_PARENT;
                    mImageView.setImageResource(R.drawable.like_list_bus);
                    int dp_5 = DensityUtil.dip2px(getActivity().getApplicationContext(), 5);
                    mImageView.setPadding(dp_5, dp_5, dp_5, dp_5);
                    mImageView.setLayoutParams(lp_ImageView);

                    int dp_10 = DensityUtil.dip2px(getActivity().getApplicationContext(), 10);
                    LinearLayout.LayoutParams lp_TextView = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
                    lp_TextView.height = ViewGroup.LayoutParams.MATCH_PARENT;
                    lp_TextView.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                    mTextView.setGravity(Gravity.CENTER_VERTICAL);
                    mTextView.setLayoutParams(lp_TextView);
                    mTextView.setTextSize(15);
                    mTextView.setTextColor(getResources().getColor(R.color.black));
                    mTextView.setPadding(dp_10,0,0,0);
                    mTextView.setText(like.getLike_route()+"路 开往"+like.getLike_end());
                    mTextView.setOnClickListener(v -> {
                        latLng = mapApplication.getLatLng();
                        BusApiUtil.toBusInfoList(latLng,getActivity(),like.getLike_route(),trafficLab);

                    });
                }
            });
        }).start();





    }
}
